<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Server extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('m_infus');
    }

    public function index() {
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');

        $config['functions']['tes'] = array('function' => 'Server.tes');

        $config['functions']['infus'] = array('function' => 'Server.infus');
        $config['functions']['pasien'] = array('function' => 'Server.pasien');
        $config['functions']['login'] = array('function' => 'Server.login');
        $config['functions']['pasienbyid'] = array('function' => 'Server.pasienbyid');
        $config['functions']['tambahinfus'] = array('function' => 'Server.tambahinfus');
        $config['functions']['cekbyidinfus'] = array('function' => 'Server.cekbyidinfus');
        $config['functions']['tambahinfus'] = array('function' => 'Server.tambahinfus');
        $config['functions']['tambahcek'] = array('function' => 'Server.tambahcek');
        $config['functions']['ubahInfus'] = array('function' => 'Server.ubahInfus');
        $config['functions']['ubahCek'] = array('function' => 'Server.ubahCek');
        $config['functions']['deleteInfus'] = array('function' => 'Server.deleteInfus');
        $config['functions']['deleteCek'] = array('function' => 'Server.deleteCek');

        $config['functions']['ubahalarm'] = array('function' => 'Server.ubahalarm');

        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }

    function tes($request) {
        $parameters = $request->output_parameters();
        return $this->xmlrpc->send_response($parameters[0]);
    }

    function infus($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->infus();
        $response = json_encode($data);
        return $this->xmlrpc->send_response($response);
    }

    function pasien($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->pasien();
        $response = json_encode($data);
        return $this->xmlrpc->send_response($response);
    }
    
    function login($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->login($parameters[0], $parameters[1]);
        return $this->xmlrpc->send_response($data);
    }

    function pasienbyid($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->pasienbyid($parameters[0]);
        $response = json_encode($data);
        return $this->xmlrpc->send_response($response);
    }

    function cekbyidinfus($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->cekbyidinfus($parameters[0]);
        $response = json_encode($data);
        return $this->xmlrpc->send_response($response);
    }

    function tambahinfus($request) {
        $parameters = $request->output_parameters();

        $now = date("Y-m-d H:i:s");
        $currentDate = strtotime($now);
        $futureDate = $currentDate + (60 * $parameters[6]);
        $di['id_user'] = $parameters[0];
        $di['id_pasien'] = $parameters[1];
        $di['waktu_pasang'] = $now;
        $di['jenis_jarum'] = $parameters[3];
        $di['tetesan']=$parameters[4];
        $di['volumeinfus'] = $parameters[5];
        $di['kecepatan'] = $parameters[6];
        $di['estimasi_habis'] = $futureDate;
        $di['catatan'] = $parameters[8];
        $di['alarm'] = $futureDate;
        
        $data = $this->m_infus->tambahInfus($di);
        $response = json_encode($data);
        if($data == true){
            $response = 1;
        } else{
            $response = 0;
        }
        return $this->xmlrpc->send_response($response);
    }

    function tambahCek($request) {
        $parameters = $request->output_parameters();

        $now = date("Y-m-d H:i:s");
        $currentDate = strtotime($now);
        //$futureDate = $currentDate+(60*$parameters[4]);
        $di['id_infus'] = $parameters[0];
        $di['waktu_cek'] = $now;
        $di['sisa_infus'] = $parameters[1];
        $di['catatan'] = $parameters[2];

        $data = $this->m_infus->tambahcek($di);
        //$response = json_encode($data);
        return $this->xmlrpc->send_response($data);
    }

    function ubahInfus($request) {
        $parameters = $request->output_parameters();

        $now = date("Y-m-d H:i:s");
        $currentDate = strtotime($now);
        //$futureDate = $currentDate+(60*$parameters[4]);
        $di['id_infus'] = $parameters[0];
        $di['waktu_cek'] = $now;
        $di['sisa_infus'] = $parameters[1];
        $di['catatan'] = $parameters[2];

        $data = $this->m_infus->tambahcek($di);
        //$response = json_encode($data);
        return $this->xmlrpc->send_response($data);
    }

    function ubahalarm($request) {
        $parameters = $request->output_parameters();
        
        $di['alarm'] = $parameters[0];
        $id_infus = $parameters[1];
        $data = $this->m_infus->ubahAlarm($di,$id_infus);
        //$response = json_encode($data);
        return $this->xmlrpc->send_response($data);
    }

    function ubahCek($request) {
        
    }

    function deleteInfus($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->deleteInfus($parameters[0]);
        return $this->xmlrpc->send_response($data);
    }

    function deleteCek($request) {
        $parameters = $request->output_parameters();
        $data = $this->m_infus->deleteCek($parameters[0]);
        return $this->xmlrpc->send_response($data);
    }

}
